using Sirenix.OdinInspector.Editor;
using UnityEditor;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay.Editors
{
    [CustomEditor(typeof(MaterialChangeRegion))]
    public class MaterialChangeRegionEditor : OdinEditor
    {
        private MaterialChangeRegion changeRegion;
        protected override void OnEnable()
        {
            changeRegion = target as MaterialChangeRegion;
            var rigidBody = changeRegion.GetComponent<Rigidbody>();
            if (rigidBody == null)
            {
                rigidBody = changeRegion.gameObject.AddComponent<Rigidbody>();
                rigidBody.isKinematic = true;
                rigidBody.useGravity = false;
            }
            base.OnEnable();
        }
    }

    public class MatChangeValueDrawer : OdinValueDrawer<MaterialChangeRegion.MatChangeValue>
    {
        protected override void DrawPropertyLayout(GUIContent label)
        {
            // 获取属性的引用
            var entry = this.ValueEntry;
            var property = this.Property;
            var variable = entry.SmartValue;

            EditorGUILayout.BeginVertical();

            variable.timeType = (MaterialChangeRegion.MatChangeTimeType)EditorGUILayout.EnumPopup("生效类型:", variable.timeType);
            variable.name = EditorGUILayout.TextField("材质属性名:", variable.name);
            variable.valueType = (MaterialChangeRegion.MatChangeValueType)EditorGUILayout.EnumPopup("材质属性类型:", variable.valueType);
            switch (variable.valueType)
            {
                case MaterialChangeRegion.MatChangeValueType.Float:
                    variable.floatValue = EditorGUILayout.FloatField("材质属性值:", variable.floatValue);
                    break;
            }

            EditorGUILayout.EndVertical();

            // 更新属性的值
            this.ValueEntry.SmartValue = variable;
        }
    }
}
